<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="成功" value="1"></el-option>
          <el-option label="失败" value="-2"></el-option>
          <el-option label="待执行" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <div class="action-btns">
      <el-button-group>
        <el-button type="info" size="mini" icon="el-icon-refresh" @click="search">刷新</el-button>
        <el-button type="primary" size="mini" icon="el-icon-refresh" @click="redo()">重新执行</el-button>
        <el-button type="info" size="mini" icon="el-icon-delete"
                   @click="del()">删除
        </el-button>
      </el-button-group>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="95" align="center" prop="id" />

      <el-table-column align="center" label="类名" width="200">
        <template v-slot:default="{scope,row}">
          {{ row.class }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="方法" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.method }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template v-slot:default="{scope,row}">
          <el-tag :type="row.status=='1'?'success':'danger'">{{ row.status_text }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="开始时间" width="180">
        <template v-slot:default="{scope,row}">
          {{ row.start_time }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="实际开始" width="180">
        <template v-slot:default="{scope,row}">
          {{ row.real_time ||'/' }}
        </template>
      </el-table-column>

      <el-table-column align="left" label="执行结果" width="300">
        <template v-slot:default="{scope,row}">
          {{ row.result||'/' }}
        </template>
      </el-table-column>


      <el-table-column align="center"   label="操作" width="150"  >
        <template v-slot:default="{scope,row}">
          <el-row>
            <el-button size="mini" type="primary" icon="el-icon-edit" circle
                       @click="$router.push({path:'queue_form',query:{id:row.id}})"
            ></el-button>
            <el-button v-if="row.status==-2" size="mini" title="重新执行" type="primary" icon="el-icon-refresh" circle
                    @click="redo(row.id)"
            ></el-button>
            <el-button size="mini" type="info" icon="el-icon-delete" circle @click="del(row.id)"></el-button>

          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/queue'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'gray',
        '-1': 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      multipleSelection: [],
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {

        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },
    del(ids) {
      if (!ids) {
        ids = this.multipleSelection.map(item => item.id)
      }
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ ids: ids }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    redo(ids) {
      if (!ids) {
        ids = this.multipleSelection.map(item => item.id)
      }
      this.$confirm('要重新执行吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.re_do({ ids: ids }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }
  }

}
</script>
